﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Runtime.InteropServices;

namespace pc_system.Model
{
    /// <summary>
    /// 构造方法参数printer_name为打印数据，  参数分别是流水号，购物清单，付款方式，总计，实收 ，
    /// 其中购物清单形如{  "书 " ," 1.00  3  3.00","杯 " ," 1.00  3  3.00"};
    /// 打印调用方式 new Printer(order_id , order_list, pay_kind , pay_sum , shishou).printer();
    /// </summary>
    class Printer
    {
        [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool GetDefaultPrinter(StringBuilder pszBuffer, ref int pcchBuffer);

        [DllImport("Winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool SetDefaultPrinter(string printerName);

            private int cy = 0;
            private int x = 0;
            private int y = 0;
            private int dy = 20;
            Font f = new Font("微软雅黑", 9);
            Brush b = new SolidBrush(Color.Black);

            public string order_id;
            public string[] order_List;
            public string pay_kind;
            public double pay_sum;
            public double shishou;

            public Printer(string order_id,string []order_List, string pay_kind, double pay_sum, double  shishou) {
                this.order_id = order_id;
                this.order_List = order_List;
                this.pay_kind = pay_kind;
                this.pay_sum = pay_sum;
                this.shishou = shishou;
            }

            public void printer()
            {
                int pcchBuffer = 0;
                StringBuilder pszBuffer = new StringBuilder(pcchBuffer);
                if (GetDefaultPrinter(pszBuffer, ref pcchBuffer))
                {
                     SetDefaultPrinter( pszBuffer.ToString());
                }


                PrintDocument pd = new PrintDocument();
                PaperSize pageSize = new PaperSize("First custom size", 400, 600);
                pd.PrinterSettings.DefaultPageSettings.PaperSize = pageSize;
                //EnumPrintProcessors 枚举系统中可用的打印处理器 
                //pd.PrinterSettings.PrinterName = "POS58 10.0.0.4";

                //pd.PrinterSettings.PrinterName = printName;
                pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
                Margins margin = new Margins(0, 0, 0, 0);
                pd.DefaultPageSettings.Margins = margin;

                //打印预览
                //PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
                //printPreviewDialog1.Document = pd;
                //printPreviewDialog1.ShowDialog();

                    //打印
                try
                {

                   pd.Print();

                }

                catch (Exception ex)
                {

                    MessageBox.Show(ex.Message, "打印出错",

                    MessageBoxButtons.OK, MessageBoxIcon.Error);

                    pd.PrintController.OnEndPrint(pd, new PrintEventArgs());

                }

 
        }

            private void pd_PrintPage(object sender, PrintPageEventArgs e)
            {
                DateTime dt = DateTime.Now;
                double zl = shishou - pay_sum;
               
                int n = this.order_List.Length;
                int i = 0, j=0;
                Graphics g = e.Graphics;
                Image logo = pc_system.Resource1.logo;
                Rectangle lg = new Rectangle(x + 50, cy, logo.Width, logo.Height);
                e.Graphics.DrawImage(logo, lg, 0, 0, logo.Width, logo.Height, System.Drawing.GraphicsUnit.Pixel);
                cy += logo.Height;
                drawLine("收银员:" + GlobalVariables.STAFF_NAME + "(" + GlobalVariables.STORE_ID + ")" , g);
                drawLine("消费流水号:" + order_id, g);
                drawLine("打印时间:" + dt, g);

                drawLine("-------------------------------------------------------", g);
                drawLine("商店名称    单价   数量  小计   ", g);
                for (i = 0; i < n ; i++)
                {
                    if (i % 2 == 0)
                    {
                        drawLine(order_List[i], g);
                    }
                    else
                    {
                        g.DrawString(order_List[i], f, b, 68, cy);
                        cy += dy;
                    } 
                                        
                }

                drawLine("-------------------------------------------------------", g);

                drawLine("支付方式：" + pay_kind, g);
                drawLine("总计：" + pay_sum.ToString("0.00"), g);
                drawLine("实收：" + shishou.ToString("0.00"), g);
                
                drawLine("找零：" + zl.ToString("0.00") , g);

                cy += 10;
                Image temp = pc_system.Resource1.qrcode;
                Rectangle re = new Rectangle(30, cy, temp.Width, temp.Height);
                e.Graphics.DrawImage(temp, re, 0, 0, temp.Width, temp.Height, System.Drawing.GraphicsUnit.Pixel);

                cy += temp.Height + 10;
                g.DrawString("扫一扫，发现更多", f, b, x + 42, cy);

            }

            private void drawLine(string  str, Graphics g) {

                g.DrawString(str, f, b, x, cy);
                cy = dy + cy;
            }
    }
}